import axios from 'axios'
import API from './contants'

// 获取用户列表
export function getUserList(params) {
  return axios.get(API.USER_LIST, { params })
    .then(response => response.data)
    .catch(error => {
      console.error('Failed to get user list:', error)
      throw error
    })
}

// 新增用户
export function addUser(data) {
  return axios.post(API.USER_ADD, data)
    .then(response => response.data)
    .catch(error => {
      console.error('Failed to add user:', error)
      throw error
    })
}

// 删除用户
export function deleteUser(id) {
  return axios.delete(`${API.USER_DELETE}/${id}`)
    .then(response => response.data)
    .catch(error => {
      console.error('Failed to delete user:', error)
      throw error
    })
}

// 批量删除用户
export function deleteUsers(ids) {
  return axios.delete(API.USER_DELETE_BATCH, { params: { ids }})
    .then(response => response.data)
    .catch(error => {
      console.error('Failed to delete users:', error)
      throw error
    })
}

// 更新用户
export function updateUser(id, data) {
  const url = API.USER_UPDATE.replace('{id}', id);
  return axios.put(url, data)
    .then(response => response.data)
    .catch(error => {
      console.error('Failed to update user:', error)
      throw error
    })
}




export default {
  getUserList,
  addUser,
  deleteUser,
  deleteUsers,
  updateUser
}
